package com.wzu.dao;

import com.wzu.pojo.BorrowRecord;
import com.wzu.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class BorrowRecordDaoImpl implements BorrowRecordDao {
    @Override
    public List<BorrowRecord> getAllBorrowRecords() {
        String sql = "SELECT * FROM borrow_records";
        List<BorrowRecord> recordList = new ArrayList<>();
        try (Connection conn = JDBCUtils.getConnection();
             PreparedStatement ps = conn.prepareStatement(sql)) {
            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    BorrowRecord record = new BorrowRecord();
                    record.setRecord_id(rs.getInt("record_id"));
                    record.setUser_id(rs.getInt("user_id"));
                    record.setBook_id(rs.getInt("book_id"));
                    record.setBorrow_date(rs.getDate("borrow_date"));
                    record.setReturn_date(rs.getDate("return_date"));
                    record.setIs_returned(rs.getBoolean("is_returned"));
                    recordList.add(record);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return recordList;
    }
}